/*
 * folder.c
 *
 *  Created on: 12 Oct 2012
 *      Author: michael
 */

#define _XOPEN_SOURCE 500
#include <ftw.h>

#include "../../include/shredder.h"

int (*callback)(const char *);

int _shredder_recurse_folder_callback(const char *filename, const struct stat *filename_stats, int information, struct FTW *ftw_information) {
	if(callback(filename)) {
		return -1;
	}
	return 0;
}

void shredder_recurse_folder(const char *filename, int (*fn)(const char *), ShredFSFlags flags) {
	int ftw_flags = 0;
	switch(flags) {
	case SHREDDER_FS_STAY_ON_FILE_SYSTEM_NO_SYMBOLIC_LINKS: {
		ftw_flags = FTW_MOUNT | FTW_PHYS;
		break;
	}
	case SHREDDER_FS_STAY_ON_FILE_SYSTEM: {
		ftw_flags = FTW_MOUNT;
		break;
	}
	case SHREDDER_FS_NO_SYMBOLIC_LINKS: {
		ftw_flags = FTW_PHYS;
		break;
	}
	case SHREDDER_FS_NO_RESTRICTIONS: {
		ftw_flags = 0;
		break;
	}
	}

	callback = fn;

	nftw(filename, _shredder_recurse_folder_callback, SHREDDER_MAX_FDS, ftw_flags);

}
